<?php

require_once getcwd().'/models/SearchFieldsShotModel.php';
require_once getcwd().'/service.impl/SearchManager.php';
require_once getcwd().'/library/Log.php';
require_once getcwd().'/library/Security.php';

class SearchController extends MyController {
	private $manager;
	
	public function __construct(){
		$this->manager = new SearchManager();
	}
	
	/*
	 * si id exists alors requete privee
	 * sinon requete publique.
	 */
	public function getAction($request){
		//private request
		$vardate = date("d/m/Y-H:i:s");
		error_log($vardate ." Search GET : \n" , 3, "log/mes-erreurs.log");
		if(isset($request->url_elements[2])){							
			return $this->getResults();	
		}		
	}
	
	private function searchDate($qObject){
		$vardate = date("d/m/Y-H:i:s");
		error_log($vardate ." Search GET Date Request : ".$qObject ."\n" , 3, "log/mes-erreurs.log");
		switch($qObject->date_count) {
			case 1 : return $this->getSearchByYear($qObject->year);
			case 2 : return $this->getSearchByYearMonth($qObject->year, $qObject->month);
			case 3 : return $this->getSearchByDate($qObject->year, $qObject->month, $qObject->day);
		}
	}
	
	private function getResults(){
		$vardate = date("d/m/Y-H:i:s");
		error_log($vardate ." Search getResult: \n" , 3, "log/mes-erreurs.log");
		$dateArray = array('','','');		
		$tagQuery = "";
		$id ="";
		if(isset($_GET['q'])){
                    $tagQuery = $_GET['q'];
                    Log::write($tagQuery);    
                }
		if(isset($_GET['date'])){
			$date = Security::format($_GET['date']);
                        Log::write($date);
			$dateArrayTemp = explode('-',$date);
			for($i=0;$i<count($dateArrayTemp);$i++){
				$dateArray[$i]=$dateArrayTemp[$i];
			}
		}
		if(isset($_GET['id'])){
			$id = $_GET['id'];
                }
                
		$objquery = new SearchFieldsShotModel($dateArray[0], $dateArray[1], $dateArray[2],$tagQuery,$id);
		Log::write("requete sur : ".$objquery);
		$res = $this->manager->get($objquery);
		Log::write(" result type : ".gettype($res));
		
		/*if($objquery->isTagQuery() and !$objquery->isDateQuery()){
			return $this->getSearchByTag($objquery);							
		}
		else if($objquery->isDateQuery() and !$objquery->isTagQuery()){
			return $this->searchDate($objectquery);			 			
		}		
		*/
		$dump = Log::var_dump($res);
		Log::write("result : ". $dump);
		
		if(empty($res)){
			Log::write("No object returned ");
			$data['message'] = "No Object";
			$data['code'] = 204;
		} 
		else if(!$res){
			Log::write("Internal error ");
			$data['message'] = "Internal Error";
			$data['code'] = 500;
		}	 
		else {
			Log::write("Success");
			$data["message"] =$res;
			$data['code'] = 200;
		}
		Log::write("Fin SearchController::getResults");
		return $data;	 
	}
	
	
	private function getSearchByTag($object){
		$vardate = date("d/m/Y-H:i:s");
		error_log($vardate ." Search GET TagRequest: ". $object->tag . "\n" , 3, "log/mes-erreurs.log");
		$dao = $this->manager;
		$result = $dao->get($object);				
		
		if(empty($result)){
			$data['message'] = "No object";
			$data['code'] = 500;
		}
		else 	
			if($result == false){				
				$data['message'] ="Error";
				$data['code'] = 500;
			}
			else {
				$data['message'] = $result;
				$data['code'] = 200;
			}
		
		return $data;
	}
	
	private function getSearchByYear($year){
		return $data["message"] = "Year";
		
	}
	private function getSearchByYearMonth($year, $month){
		return $data["message"] = "Year Month";
	}
	private function getSearchByDate($year, $month, $day){
		return $data["message"] = "Date";
	}
	
	public function postAction($request){
		
	}
}

?>